
<?php

  // 判断$_POST不为空
  if(!empty($_POST)){


   // 表单提交数据存于$_POST中
  $username = $_POST['username'];
  $password = md5($_POST['password']);
  $code = strtolower($_POST['code']);

  // 优化勾选了记住按钮的状态下实现使用cookie的密码进行登录
  if(!empty($_COOKIE['username']) && $_POST['password']==$_COOKIE['password']){
  	$password = $_COOKIE['password'];
  }

  // //调用查询函数
  // $where['username'] = $username;
  // $where['password'] = $password;
  $true = getSelectByOne('admin`,`role',"`admin`.role_id = `role`.id and `username`='$username' and `password`='$password' and endble=0",'admin.*,role.name,role.power');


      // 判断变量是否为空来提示登录是否成功
      if(!empty($true)){

          $true['power'] = explode(',',$true['power']);

          // 给SESSION下标赋值
          $_SESSION['admin']=$true;
          // 判断用户是否勾选‘记住’按钮
          if(!empty($_POST['check'])){
            setcookie('username',$true['username'],time()+3600*24);
            setcookie('password',$true['password'],time()+3600*24);
            setcookie('check','checked',time()+3600*24);

          }else{
          	setcookie('username','',0);
            setcookie('password','',0);
            setcookie('check','',0);
          };

          // 判断用户是否勾选‘自动登录’
          if(!empty($_POST['auto_login'])){
            //将用户勾选自动操作的记录记录起来
             setcookie('auto_login',1,time()+3600*24*7);


          }else{
            setcookie('auto_login','',0);

          };

          // // 判断post的验证码是否和session的验证码一致
          // if(strtolower($_SESSION['code'])!=$code){
          //   echo"<script>alert('验证码错误');history.go(-1);</script>";die;
          // }

          // unset($_SESSION['code']);
/*
      	echo"<script>alert('登录成功'); location.href='index.php'</script>";die; //js实现页面跳转方法*/
        header('location:index.php?c=index&a=index&admin=1');//php实现页面跳转方法   优势：更快
      }else{
      	echo"<script>alert('用户名或密码错误');history.go(-1)</script>";die;
      }

  }

//加载后台页面视图
 include View_PATH.$admin.'/admin/login.html';


?>
